package www.golf.web.gonwubu.edit;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Toolbarbutton;

import www.golf.web.gonwubu.ConnectionControl;
import www.golf.web.session.WebSession;
import www.golf.web.util.UtilZKObject;

public class ConnectionFinal extends WebSession{
	UtilZKObject ZKObj =new UtilZKObject(this);
	Map argmap= Executions.getCurrent().getArg();
	public void initialize() {
	}
	public void upload(UploadEvent evt){
		if(getFellow("vbox_fileList").getChildren().size()>0)getFellow("vbox_fileList").getFirstChild().detach();
		Media[] medias=null;	
		medias = evt.getMedias();
		upload(medias);
	}
	private void upload(Media[] medias){
	    for(Media obj : medias){
	      String name[]=obj.getName().split("\\.");
	      if(!(name[name.length-1].equals("rar")||name[name.length-1].equals("RAR")
	    		  ||name[name.length-1].equals("zip")||name[name.length-1].equals("ZIP"))){
	    	  new Label("檔案格式錯誤,接受rar,zip格式").setParent(getFellow("vbox_fileList"));
	    	  break;
	      }
	      byte[] file_byte=null;
	      //用來記錄檔案的大小
	      int size = 0;
	      //檔案的IO處理,將檔案從media透過inputstream 和outputstream來做傳輸
	      try {
	        //FileOutputStream fos = new FileOutputStream(new File(uploadFolderPath + obj.getName()));
	    	ByteArrayOutputStream bos=new ByteArrayOutputStream();
	        InputStream is = obj.getStreamData();
	        byte[] buffer = new byte[1024];
	        int pointer = 0;
	        while((pointer = is.read(buffer))!= -1){
	          size += pointer;
	          bos.write(buffer, 0, pointer);
	        }
	        if(is != null){
	          is.close();
	        }
	        if(bos != null){
	        	file_byte=bos.toByteArray();
	        	bos.flush();
	        	bos.close();
	        }
	      } catch (Exception e) {
	    	 new Label("上传失败,檔案可能過大").setParent(getFellow("vbox_fileList"));
	        e.printStackTrace();
	      } 
	      Hbox hb = new Hbox();
	      hb.setAttribute("data", file_byte);
	      hb.setParent(getFellow("vbox_fileList"));
	      Toolbarbutton tbb = new Toolbarbutton();
	      tbb.setLabel(obj.getName());
	      tbb.setHref(obj.getName());
	      tbb.setParent(hb);
	      Label lb = new Label();
	      lb.setValue(size/1024 + " KB");
	      lb.setParent(hb);
	    }
	  }
	public void save() throws Exception {
		HashMap<String, Object> map=new HashMap<String, Object>();
		map.put("pid", Integer.parseInt((String)argmap.get("pid")));
		map.put("data", getFellow("vbox_fileList").getFirstChild().getAttribute("data"));
		map.put("books", ZKObj.getValue("edbook"));
		map.put("data_type", Integer.parseInt((String)argmap.get("data_type")));
		dbSession().update("Connection.putConnection",map);
		Messagebox.show("归挡完毕!");
		view_refresh();
		this.detach();
	}
	public void view_refresh(){
		Iterator it=Executions.getCurrent().getDesktop().getPage("main_content").getFellows().iterator();
		ConnectionControl win=(ConnectionControl)getm_content().getFellow("p_w");
		win.initialize();
	}
}
